window['AddressInfoInit'] = function(){
	if(!window['address-info'])return;
	$('#js-id').val(window['address-info'].id);
	$('#js-zipcode').val(window['address-info'].zipcode);
	$('#js-realname').val(window['address-info'].realname);
	$('#js-phone').val(window['address-info'].phone);
	$('#js-location').val(window['address-info'].location);
	$('#js-default').prop('checked',window['address-info'].default);
		
	
	if($('#js-province').val() != '' && $('#js-realname').val() == ''){
		$('#js-province').find("option:selected").removeAttr('selected');
	}
           
	function verify(attr,value){
		switch (attr){
			case 'id':
				return "";
			break;
			case 'location'	:
				if(value=="")
					return "请填写详细的收货地址！";
				if(!/^.{5,40}$/.test(value))
					return "请填写正确的收货地址！";
				return "";
			break;
			case 'zipcode'	:
				if(value!="")
					if(!/^\d+$/.test(value))
						return "邮政编码格式错误！";
				return '';
			break;
			case 'phone'	:
				if (value=='') return "请填写手机！";
				if(!/^\d{11,11}$/.test(value))
					return "手机格式错误！";
				return '';
			break;
			case 'realname'		:
				if(value=="")
					return "请填写收货人姓名！";
				if(!/^[\u4e00-\u9fa5]{2,10}$/.test(value))
					return "请填写正确的收货人姓名！";
				return '';
			break;
			default			:
				if(value==''){
					return '请选择城市区域!';
				}
				return '';
			break;
		}
	};
	var ajaxCount = 0;
	function ajax(n,value,value2,callback){
		var _ajaxCount = ++ajaxCount;
		switch (n){
			case 1:
				$('#js-area-box').hide();
				$('#js-city-box').show();
				$('#js-city,#js-area').html('<option value="">正在加载...</option>');
			break;
			case 2:
				$('#js-area-box').show();
				$('#js-area').html('<option value="">正在加载...</option>');
			break;
		}
               
		$.ajax({
			url	:'/member/address',
			data	:{act:'more',n:n,higher:value},
//			url	:'ajax.php?act=address_'+n,
//			data	:{act:'address_'+n,higher:value},
			type	:'POST',
			error	:function(){if(_ajaxCount!=ajaxCount)return;if(callback){callback(false);}},
			success	:function(data){
				if(_ajaxCount!=ajaxCount)return;
				try{
					data = $.parseJSON(data);
					if(data.List&&data.List.length){
						var html = '';
						$.each(data.List,function(index,value){
							html+='<option '+((value2==value.Value)?'selected':'')+' value="'+value.Value+'" code="'+value.Code+'">'+value.Name+'</option>';
//							if(n == 2 && value2==value.Value&&!(window['address-info'].id >0))
//							{
//							   $('#js-zipcode').val(value.Code); 
//							}
						});
						switch (n){
							case 1:
								$('#js-city').html('<option value="">请选择城市</option>'+html).change();
							break;
							case 2:
								$('#js-area').html('<option value="">请选择区域</option>'+html).change();
							break;
						}
						if(callback){callback(true);}
					}else{
            
						switch (n){
							case 1:
								$('#js-city-box,#js-area-box').hide().find('select').html('<option value="0">无下级</option>');
							break;
							case 2:
								$('#js-area-box').hide().find('select').html('<option value="0">无下级</option>');
							break;
						}
						if(callback){callback(false);}
					}
				}catch (e){if(callback){callback(false);}}
			}
		});
	};
	$('#js-province,#js-city,#js-area').unbind('change').bind('change',function(){
		if($(this).val()!=''){
			if($(this).find('option:first').attr('value')==''){
				$(this).find('option:first').remove();
			}
		}
	});
    
	var isload = false;
	var html = '';
	$.each(ProvinceList,function(index,province){
		html+='<option '+((window['address-info'].province==province.Value)?'selected':'')+' value="'+province.Value+'">'+province.Name+'</option>';
	});
             
	$('#js-province').append(html).find('option:first').html('请选择省份').end().change();
	if($('#js-province').val()!=''){
		ajax(1,window['address-info'].province,window['address-info'].city,function(isok){
			if(isok){
				ajax(2,window['address-info'].city,window['address-info'].area,function(){
					isload = true;
				});
			}else{
				isload = true;
			}
		});
	}else{
		isload = true;
		$('#js-city-box,#js-area-box').hide();
	}
	$('#js-province').unbind('change').bind('change',function(){
		if(isload&&$(this).val()!=''){
			ajax(1,$(this).val());
		}
	});
	$('#js-city').unbind('change').bind('change',function(){
		if(isload&&$(this).val()!=''){
			ajax(2,$(this).val());
		}
	});
        $('#js-area').unbind('change').bind('change',function(){
		if(isload&&$(this).val()!=''){
			$('#js-zipcode').val($(this).find('option:selected').attr('code'));
		}
	});
	window['address-info-success'] = window['address-info-success'] || function(data){
		window.location.href=tourl+"?address_id="+data['id'];
	};
	var issave = false;
	var button = $('#js-save').unbind('click').bind('click',function(){
		if(issave)return;
		var error = '';
		var savedata = {};
		$.each(['id','realname','province','city','area','location','phone'],function(index,key){
			savedata[key] = $.trim($('#js-'+key).val());
			error = verify(key,savedata[key]);
			if(error){
				return false;
			}
		});
		if(error){
			alert(error);
			return false;
		}
		$(this).val('正在保存...');
		var self = this;
		savedata.default = $('#js-default').prop('checked');

		$.ajax({url:'/member/address?act=save',type:'POST',data:savedata,error:function(){
			alert('链接服务器失败，请稍后再试！');
			$(self).val(button);
		},success:function(data){
			try{
            	data = $.parseJSON(data);
			}catch (e){
				alert('链接服务器失败，请稍后再试！');
				$(self).val(button);
				return;
			}
			if(data.IsSuccess){
				window['address-info-success'](data.address);
			}else{
				alert(data.Message);
				$(self).val(button);
			}
		}});
	}).val();
};
$(document).ready(function(e) {
	//地址添加 修改
	if ($('#js-province').length>0) window['AddressInfoInit']();

	//选择收货地址
	$('#consigneelist li').click(function(){
		$(this).find('form').submit();
	});
	
	$('.addressform select').live('change',function(){
		$(this).parent().next().val($(this).val());
	});
	
	
	//删除地址
	$('#consigneelist li i').click(function(){
		var $li=$(this).parent();
		var id=$li.find('input').val();
		$.ajax({
			url: '/member/address?ajax=1&act=del&id='+id,
//			url: 'ajax.php?act=deladd',
			type: 'GET',
			dataType: 'json',
			cache:false,
			timeout: 50000,
			error: function(data){},
			success: function(data){
				if(data.IsSuccess){
					$li.remove();
				}else{
					alert(data.Message);	
				}
			}
		});	
		return false;
	});
	
	
});
